package com.szcinda.service;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    private static final String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    static final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    static final DateTimeFormatter df_2 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    public static final DateTimeFormatter df_cn = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
    public static final DateTimeFormatter df_time = DateTimeFormatter.ofPattern("HH:mm");

    public static final DateTimeFormatter crossDf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    static final DateTimeFormatter diagonalDf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    static final DateTimeFormatter version_df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");


    public static LocalDateTime parseLocalDateTime(String dateStr) {
        return LocalDateTime.parse(dateStr, df);
    }
    public static LocalDate parseLocalDate(String dateStr) {
        return LocalDate.parse(dateStr, df);
    }
    public static String parseLocalDate2Str(LocalDate date) {
        if(date == null){
            return "";
        }
        return date.format(df_2);
    }

    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    public static LocalDate toLocalDate(Object str){
        if (str == null) {
            return null;
        }
        try {
            Date date = parseDate(str.toString(), parsePatterns);
            Instant instant = date.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            return instant.atZone(zoneId).toLocalDate();
        } catch (Exception e) {
            return null;
        }
    }

    public static LocalDateTime convert(String date) {
        try {
            if (StringUtils.isEmpty(date)) {
                return null;
            }
            if (date.length() < 17) {
                date = String.format("%s:00", date);
            }
            if (date.contains("-")) {
                return LocalDateTime.parse(date, crossDf);
            } else if (date.contains("/")) {
                return LocalDateTime.parse(date, diagonalDf);
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    public static String getDateString() {
        return LocalDateTime.now().format(version_df);
    }

    // 通过此方法，将字符串的日期格式化
    public static LocalDateTime convertStringToLocalDateTime(String text) {
        try {
            String[] dateArr = text.split(" ");
            String dateStr = dateArr[0];
            String timeStr = dateArr[1];
            String year = null;
            String month = null;
            String day = null;
            dateStr = dateStr.replaceAll("/", "-");
            if (dateStr.contains("-")) {
                String[] dateSplit = dateStr.split("-");
                year = dateSplit[0];
                month = dateSplit[1];
                day = dateSplit[2];
                if (month.length() < 2) {
                    month = "0" + month;
                }
                if (day.length() < 2) {
                    day = "0" + day;
                }
            }
            if (timeStr.length() < 8) {
                String[] timeSplit = timeStr.split(":");
                if (timeSplit[0].length() < 2) {
                    String newDate = year + "-" + month + "-" + day + " 0" + dateArr[1];
                    return convert(newDate);
                }
            }
            String newDate = year + "-" + month + "-" + day + " " + dateArr[1];
            return convert(newDate);
        } catch (Exception e) {
            return null;
        }
    }

    public static Date parseDate(String str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str, parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    public static void main(String[] args) {
        String text = "2022/9/16";
        Date date = DateUtils.parseDate(text);
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        System.out.println(localDate);
    }
}
